package com.zxj.camerapreview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.zxj.camerapreview.view.glview1.CameraGLSurfaceView;
import com.zxj.camerapreview.view.glview1.CameraInterface;

public class CustomGLViewActivity extends AppCompatActivity {
    private static final String TAG = CustomGLViewActivity.class.getSimpleName();
    CameraGLSurfaceView glSurfaceView = null;
    Button shutterBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_gl_view);
        glSurfaceView = findViewById(R.id.glSurfaceView);
        shutterBtn = (Button)findViewById(R.id.btn_shutter);
        initViewParams();
        //拍照
        shutterBtn.setOnClickListener(new BtnListeners());

    }

    private void initViewParams(){
        ViewGroup.LayoutParams params = glSurfaceView.getLayoutParams();
        Point p = getScreenMetrics(this);
        params.width = p.x; //view宽
        params.height = p.y; //view高
        //设置GLSurfaceView的宽和高
        glSurfaceView.setLayoutParams(params);
    }

    private Point getScreenMetrics(Context context){
        DisplayMetrics dm =context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        return new Point(w_screen, h_screen);
    }

    //拍照
    private class BtnListeners implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn_shutter:
                    CameraInterface.getInstance().doTakePicture();
                    break;
                default:break;
            }
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            Log.e(TAG,"横屏");
        }else {
            Log.e(TAG,"竖屏");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"===onResume===");
        //更改视图在树中的z顺序，因此它位于其他同级视图之上。
        glSurfaceView.bringToFront();

    }

    @Override
    protected void onPause() {
        super.onPause();
        glSurfaceView.onPause();
    }
}